---@class EventDispatch : LuaObject
local this = BaseClass("EventDispatch")

function this:__ctor()
    self.dicEvents = {}
end

---@param eType string|number
---@param eFunc function
function this:AddEventListener(eType, eFunc)
    local dicEvents = self.dicEvents[eType] or {}
    if(not eFunc)then return end

    if(not table.contains(dicEvents, eFunc))then
        table.insert(dicEvents, eFunc)
    end
end

---@param ev ZEvent
function this:DispatchEvent(ev)
    if(not ev)then return end
    local dicEvents = self.dicEvents[ev.eventName] or {}

    for k, v in dicEvents do
        v(self, ev)
    end
end

function this:RemoveEventListener(eType, eFunc)
    if(not eFunc)then return end
    local dicEvents = self.dicEvents[eType] or {}
    local key = table.keyof(dicEvents, eFunc, -1)

    if(key > 0 and key <= #dicEvents)then
        table.remove(dicEvents, key)
    end
end

function this:RemoveEventListener(eType)
    if self.dicEvents[eType] ~= nil then
        table.remove(self.dicEvents, eType)
    end
end

EventDispatch = this